﻿using System;
using System.Reflection;
using System.Configuration;
namespace YuuzleComic.DALFactory
{
	/// <summary>
    /// Abstract Factory pattern to create the DAL。
    /// 如果在这里创建对象报错，请检查web.config里是否修改了<add key="DAL" value="Maticsoft.SQLServerDAL" />。
	/// </summary>
	public sealed class DataAccess
	{
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];        
		public DataAccess()
		{ }

        #region CreateObject 

		//不使用缓存
        private static object CreateObjectNoCache(string AssemblyPath,string classNamespace)
		{		
			try
			{
				object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);	
				return objType;
			}
			catch//(System.Exception ex)
			{
				//string str=ex.Message;// 记录错误日志
				return null;
			}			
			
        }
		//使用缓存
		private static object CreateObject(string AssemblyPath,string classNamespace)
		{			
			object objType = DataCache.GetCache(classNamespace);
			if (objType == null)
			{
				try
				{
					objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);					
					DataCache.SetCache(classNamespace, objType);// 写入缓存
				}
				catch//(System.Exception ex)
				{
					//string str=ex.Message;// 记录错误日志
				}
			}
			return objType;
		}
        #endregion

        #region CreateSysManage
        public static YuuzleComic.IDAL.ISysManage CreateSysManage()
		{
			//方式1			
			//return (YuuzleComic.IDAL.ISysManage)Assembly.Load(AssemblyPath).CreateInstance(AssemblyPath+".SysManage");

			//方式2 			
			string classNamespace = AssemblyPath+".SysManage";	
			object objType=CreateObject(AssemblyPath,classNamespace);
            return (YuuzleComic.IDAL.ISysManage)objType;		
		}
		#endregion
             
        
   
		/// <summary>
		/// 创建abcd数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Iabcd Createabcd()
		{

			string ClassNamespace = AssemblyPath +".abcd";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Iabcd)objType;
		}


		/// <summary>
		/// 创建ad数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Iad Createad()
		{

			string ClassNamespace = AssemblyPath +".ad";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Iad)objType;
		}


		/// <summary>
		/// 创建admin数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Iadmin Createadmin()
		{

			string ClassNamespace = AssemblyPath +".admin";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Iadmin)objType;
		}


		/// <summary>
		/// 创建cjitem数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Icjitem Createcjitem()
		{

			string ClassNamespace = AssemblyPath +".cjitem";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Icjitem)objType;
		}


		/// <summary>
		/// 创建cjlog数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Icjlog Createcjlog()
		{

			string ClassNamespace = AssemblyPath +".cjlog";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Icjlog)objType;
		}


		/// <summary>
		/// 创建cjxm数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Icjxm Createcjxm()
		{

			string ClassNamespace = AssemblyPath +".cjxm";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Icjxm)objType;
		}


		/// <summary>
		/// 创建class数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Iycclass Createycclass()
		{

			string ClassNamespace = AssemblyPath +".ycclass";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Iycclass)objType;
		}


		/// <summary>
		/// 创建comics数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Icomics Createcomics()
		{

			string ClassNamespace = AssemblyPath +".comics";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Icomics)objType;
		}


		/// <summary>
		/// 创建diqu数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Idiqu Creatediqu()
		{

			string ClassNamespace = AssemblyPath +".diqu";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Idiqu)objType;
		}


		/// <summary>
		/// 创建link数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Ilink Createlink()
		{

			string ClassNamespace = AssemblyPath +".link";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Ilink)objType;
		}


		/// <summary>
		/// 创建news数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Inews Createnews()
		{

			string ClassNamespace = AssemblyPath +".news";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Inews)objType;
		}


		/// <summary>
		/// 创建Reply数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.IReply CreateReply()
		{

			string ClassNamespace = AssemblyPath +".Reply";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.IReply)objType;
		}


		/// <summary>
		/// 创建zj数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Izj Createzj()
		{

			string ClassNamespace = AssemblyPath +".zj";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Izj)objType;
		}


		/// <summary>
		/// 创建zjlog数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Izjlog Createzjlog()
		{

			string ClassNamespace = AssemblyPath +".zjlog";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Izjlog)objType;
		}


		/// <summary>
		/// 创建zz数据层接口。
		/// </summary>
		public static YuuzleComic.IDAL.Izz Createzz()
		{

			string ClassNamespace = AssemblyPath +".zz";
			object objType=CreateObject(AssemblyPath,ClassNamespace);
			return (YuuzleComic.IDAL.Izz)objType;
		}


        public static YuuzleComic.IDAL.ICollect CreateCollect()
        {
            string ClassNamespace = AssemblyPath + ".Collect";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (YuuzleComic.IDAL.ICollect)objType;
        }

    }
}